package com.yongninger.activity.shipsmentinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.yongninger.activity.MainActivity;
import com.yongninger.activity.R;
import com.yongninger.adapter.ShipsmentinfoListViewAdapter;
import com.yongninger.dao.ShipmentsInfoDao;
import com.yongninger.vo.ShipmentsInfoVo;

public class ListActivity  extends android.app.ListActivity implements OnScrollListener {
	private ShipmentsInfoDao shipmentsInfoDao;
	/*分页*/
	private ListView listView;
	private int visibleLastIndex = 0;	//最后的可视项索引
	private int visibleItemCount;		// 当前窗口可见项总数
	private ShipsmentinfoListViewAdapter adapter;
	private View loadMoreView;
	private Button loadMoreButton;
	private Handler handler = new Handler();
	private int pagesize = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shipsment_list);
		shipmentsInfoDao = new ShipmentsInfoDao(getApplicationContext());
		loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		listView = getListView();				//获取id是list的ListView
		listView.addFooterView(loadMoreView);	//设置列表底部视图
		initAdapter();
		setListAdapter(adapter);				//自动为id是list的ListView设置适配器
		listView.setOnScrollListener(this);		//添加滑动监听
		// 条目点击事件
		listView.setOnItemClickListener(new ItemClickListener());
		
		// 新增按钮点击事件
		Button addBtn = (Button) this.findViewById(R.id.add);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences userInfo = getSharedPreferences("user_info", 0);
				String umenu22 = userInfo.getString("umenu22", "");
				if("1".equals(umenu22)){
					Intent intent = new Intent(ListActivity.this, AddActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限编辑",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		addBtn.setOnClickListener(onClickListener);
	}

	// 获取点击事件
	private final class ItemClickListener implements OnItemClickListener {

		@SuppressWarnings("unchecked")
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ListView listView = (ListView) parent;
			HashMap<String, Object> data = (HashMap<String, Object>) listView
					.getItemAtPosition(position);
			Intent intent = new Intent(ListActivity.this, ViewActivity.class);
			ShipmentsInfoVo shipmentsInfoVo = new ShipmentsInfoVo();
			shipmentsInfoVo.setSid(Integer.parseInt((String.valueOf(data.get("smid")))));
			shipmentsInfoVo.setSname(String.valueOf(data.get("smname")));
			shipmentsInfoVo.setScode(String.valueOf(data.get("smcode")));
			shipmentsInfoVo.setScompany(String.valueOf(data.get("smcompany")));
			intent.putExtra("shipmentsInfoVo", shipmentsInfoVo);
			startActivity(intent);
		}
	}
	/**
	 * 初始化适配器
	 */
	private void initAdapter() {
		List<ShipmentsInfoVo> shipmentsInfoVos = shipmentsInfoDao.queryShipmentsInfoVoList(null,0,pagesize);
		List<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
		for (ShipmentsInfoVo vo : shipmentsInfoVos) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("smid", vo.getSid());
			item.put("smname", vo.getSname());
			item.put("smcode", vo.getScode());
			item.put("smcompany", vo.getScompany());
			items.add(item);
		}
		adapter = new ShipsmentinfoListViewAdapter(this, items);
	}

	/**
	 * 滑动时被调用
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
	}

	/**
	 * 滑动状态改变时被调用
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = adapter.getCount() - 1;	//数据集最后一项的索引
		int lastIndex = itemsLastIndex + 1;				//加上底部的loadMoreView项
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
			//如果是自动加载,可以在这里放置异步加载数据的代码
			Log.i("LOADMORE", "loading...");
		}
	}
	
	/**
	 * 点击按钮事件
	 * @param view
	 */
	public void loadMore(View view) {
		loadMoreButton.setText("loading...");	//设置按钮文字loading
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				
				loadData();
				
				adapter.notifyDataSetChanged();	//数据集变化后,通知adapter
				listView.setSelection(visibleLastIndex - visibleItemCount + 1);	//设置选中项
				
				loadMoreButton.setText("load more");	//恢复按钮文字
			}
		}, 2000);
	}
	
	/**
	 * 模拟加载数据
	 */
	private void loadData() {
		int count = adapter.getCount();
		List<ShipmentsInfoVo> shipmentsInfoVos = shipmentsInfoDao.queryShipmentsInfoVoList(null,count,pagesize);
		for (ShipmentsInfoVo vo : shipmentsInfoVos) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("smid", vo.getSid());
			item.put("smname", vo.getSname());
			item.put("smcode", vo.getScode());
			item.put("smcompany", vo.getScompany());
			adapter.addItem(item);
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ListActivity.this,
					MainActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
